package com.leyou.item.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.leyou.common.exception.pojo.ExceptionEnum;
import com.leyou.common.exception.pojo.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class SpecService {

    @Autowired(required = false)
    private SpecGroupMapper groupMapper;

    @Autowired(required = false)
    private SpecParamMapper paramMapper;


    /*根据分类id查询规格组列表*/
    public List<SpecGroup> findSpecGroupsByCid(Long id) {

        //封装条件
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(id);
        QueryWrapper<SpecGroup> wrapper = Wrappers.query(specGroup);

        List<SpecGroup> specGroups = groupMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(specGroups)){
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        return specGroups;
    }

    /*查询规格参数列表*/
    public List<SpecParam> findSpecParams(Long gid, Long cid, Boolean searching) {

        //假如cid和gid都不传，就认为参数有误。
        if(gid==null && cid==null){
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        }

        //封装条件
        SpecParam specParam = new SpecParam();
        specParam.setGroupId(gid);
        specParam.setCid(cid);
        specParam.setSearching(searching);
        QueryWrapper<SpecParam> queryWrapper = Wrappers.query(specParam);

        //数据库查询
        List<SpecParam> specParams = paramMapper.selectList(queryWrapper);
        if(CollectionUtils.isEmpty(specParams)){
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        return specParams;
    }


    //根据cid3查询 规格组集合 且每个规格组对象中要包含当前规格组对于的 规格参数集合
    public List<SpecGroupDTO> findSpecGroupDTOSByCid(Long id) {

        //======方式三========
        List<SpecGroup> specGroups = findSpecGroupsByCid(id);
        //把规格组集合转换成规格组DTO集合
        List<SpecGroupDTO> specGroupDTOS = BeanHelper.copyWithCollection(specGroups, SpecGroupDTO.class);
        List<SpecParam> specParams = findSpecParams(null, id, null);
        //对规格参数集合根据规格组id进行分组
        Map<Long, List<SpecParam>> specParamMap = specParams.stream().collect(Collectors.groupingBy(SpecParam::getGroupId));
        //遍历规格组
        specGroupDTOS.forEach(specGroup -> {
            specGroup.setParams(specParamMap.get(specGroup.getId()));
        });



        //=======方式二=======  [查询了一次数据库 , 但是循环次数过多]
//        //根据分类id查询规格组集合
//        List<SpecGroup> specGroups = findSpecGroupsByCid(id);
//        //把规格组集合转换成规格组DTO集合
//        List<SpecGroupDTO> specGroupDTOS = BeanHelper.copyWithCollection(specGroups, SpecGroupDTO.class);
//        //根据规格组id 查询规格参数集合
//        List<SpecParam> specParams = findSpecParams(null, id, null);
//
//        //遍历规格组
//        specGroupDTOS.forEach(groupDTO->{
//            //遍历规格参数
//            specParams.forEach(specParam->{
//                if (groupDTO.getId().equals(specParam.getGroupId())){
//                    groupDTO.getParams().add(specParam);
//                }
//            });
//        });


        //======方式一=========  [缺点: 需要循环进行数据库查询]
//        //根据分类id查询规格组集合
//        List<SpecGroup> specGroups = findSpecGroupsByCid(id);
//        //把规格组集合转换成规格组DTO集合
//        List<SpecGroupDTO> specGroupDTOS = BeanHelper.copyWithCollection(specGroups, SpecGroupDTO.class);
//
//        //遍历
//        specGroupDTOS.forEach(groupDTO->{
//            //根据规格组id 查询规格参数集合
//            List<SpecParam> specParams = findSpecParams(groupDTO.getId(), null, null);
//            groupDTO.setParams(specParams);
//        });

        return specGroupDTOS;
    }
}